﻿// ================================================
// TUserItem.cs, 物品基本属性结构体
// Copyright (C) 2012 Daomi
// ================================================
using System;
using System.Runtime.InteropServices;
using System.IO;

namespace SystemModule
{
    /// <summary>
    /// 物品基本属性结构体
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public class TStdItem  //Size 64
    {
        /// <summary>
        /// 物品名称
        /// </summary>
        public string Name;
        /// <summary>
        /// 物品分类 0/1/2/3：药， 5/6:武器，10/11：盔甲，15：头盔，22/23：戒指，24/26：手镯，19/20/21：项链
        /// </summary>
        public byte StdMode;
        /// <summary>
        /// 装配外观
        /// </summary>
        public byte Shape;
        /// <summary>
        /// 重量
        /// </summary>
        public byte Weight;
        public byte AniCount;
        public sbyte Source;// 源动力
        public byte Reserved;// 保留
        public byte NeedIdentify;// 需要记录日志
        public ushort Looks;// 外观，即Items.WIL中的图片索引
        public ushort DuraMax;// 最大持久
        public ushort Reserved1;// 发光属性
        public int AC;
        public int MAC;
        /// <summary>
        /// 攻击点
        /// </summary>
        public int DC;
        /// <summary>
        /// 魔法点
        /// </summary>
        public int MC;
        /// <summary>
        /// 道术点
        /// </summary>
        public int SC;
        /// <summary>
        /// 条件
        /// </summary>
        public int Need;
        /// <summary>
        /// 条件等级
        /// </summary>
        public int NeedLevel;
        /// <summary>
        /// 价格
        /// </summary>
        public int Price;
        public int Stock;

        public byte[] ToByte()
        {
            using (var memoryStream = new MemoryStream())
            {
                var backingStream = new BinaryWriter(memoryStream);
                int nameLen = 0;
                byte[] nameBuff = HUtil32.StringToByteAry(Name, out nameLen);
                nameBuff[0] = (byte)nameLen;
                Array.Resize(ref nameBuff, 15);
                backingStream.Write(nameBuff);
                backingStream.Write(StdMode);
                backingStream.Write(Shape);
                backingStream.Write(Weight);
                backingStream.Write(AniCount);
                backingStream.Write(Source);
                backingStream.Write(Reserved);
                backingStream.Write(NeedIdentify);
                backingStream.Write(Looks);
                backingStream.Write(DuraMax);
                backingStream.Write(Reserved1);
                backingStream.Write(AC);
                backingStream.Write(MAC);
                backingStream.Write(DC);
                backingStream.Write(MC);
                backingStream.Write(SC);
                backingStream.Write(Need);
                backingStream.Write(NeedLevel);
                backingStream.Write(Price);
                backingStream.Write(Stock);

                return memoryStream.ToArray();
            }
        }


    }
}
